package com.lyy.weather.fragment;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lyy.weather.R;
import com.lyy.weather.activity.ChangeCityActivity;
import com.lyy.weather.activity.OnItemListener;
import com.lyy.weather.bean.City;
import com.lyy.weather.database.DatabaHelper;
import com.lyy.weather.database.DatabaseUtil;
import com.lyy.weather.layout.adapter.ChangeCityAdapter;
import com.lyy.weather.layout.bean.CityChange;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class WeatherChangeFragment extends Fragment {
    private Activity activity;
    private Button addingCityBTN;
    private FragmentManager fragmentManager;

    public WeatherChangeFragment(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view=inflater.inflate(R.layout.weather_change_fragment,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity=getActivity();

        addingCityBTN=(Button)activity.findViewById(R.id.adding_button_change_fragment);
        addingCityBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //更换到添加城市碎片
                replaceFragment(new WeatherAddingFragment(fragmentManager));

            }
        });
        showCityList();//展示城市列表


    }


    private void showCityList() {
        //展现已有城市列表
        DatabaHelper dbHelper = new DatabaHelper(getActivity(), "WeatherInformation.db", null, 31, null);
        DatabaseUtil dbUtil = new DatabaseUtil(getActivity(), dbHelper);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        List<CityChange> cityChangeList = new ArrayList<>();
        List<String> locationIdList = dbUtil.getAlllocationId();
        if (locationIdList != null) {
            Iterator<String> it = locationIdList.iterator();

            while (it.hasNext()) {
                String locationId = it.next();
                City city = (City) dbUtil.searchCity(locationId);
                CityChange cityChange = new CityChange();
                cityChange.setName(city.getName());
                cityChange.setAdm1(city.getAdm1());
                cityChange.setAdm2(city.getAdm2());
                cityChange.locationId = city.getId();
                cityChangeList.add(cityChange);
            }

            Log.d("TAG", cityChangeList.toString());
            ChangeCityAdapter adapter = new ChangeCityAdapter(cityChangeList);
            adapter.setOnItemClickListener(new OnItemListener() {
                @Override
                public void onItemClick(View view, int position, String locationId) {
                    Intent intent = new Intent();
                    intent.putExtra("locationId", locationId);
                    activity.setResult(RESULT_OK, intent);
                    activity.finish();
                }
            });
            RecyclerView view = (RecyclerView) activity.findViewById(R.id.city_recyclerview_change);
            //LayoutManager用于指定RecyclerView的布局方式，这里使用的是线性布局，可以实现和ListView类似的效果
            LinearLayoutManager layoutManager = new LinearLayoutManager(activity);

            view.setLayoutManager(layoutManager);
            //创建适配器实例，传入子项数据
            //  ChangeCityAdapter adapter=new ChangeCityAdapter(cityChangelist);
            view.setAdapter(adapter);
        }
    }
    /**
     * 更换碎片
     */
    public void replaceFragment(Fragment fragment){

        //3.开启一个事务，通过调用beginTransaction()方法开启
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        //4.向容器内添加或替换碎片，一般使用replace()方法实现，需要传入容器的id和待添加的碎片实例
        transaction.replace(R.id.weather_change_fragment_layout,fragment);
        //5.提交事务，调用commit()方法完成
        transaction.commit();
    }


}



